package com.spm.librarianmanagersystem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spm.librarianmanagersystem.pojo.Book;
import com.spm.librarianmanagersystem.service.BookInfoService;
import com.spm.librarianmanagersystem.service.BookService;
import com.spm.librarianmanagersystem.utils.FormatUtil;
import com.spm.librarianmanagersystem.utils.JsonResult;
import com.spm.librarianmanagersystem.vo.BookDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 苏成
 * @Date: 2022/04/05/11:49
 * @Description:
 */
@RestController
@RequestMapping("/api/admin")
public class BookController {

    @Autowired
    private BookService  bookService;

    @Autowired
    BookInfoService bookInfoService;
    @Autowired
    FormatUtil formatUtil;

    // 管理员登录后获取全部的图书信息
    @GetMapping("/book/getAll")
    public JsonResult<JSONArray> getAll(@RequestParam("page_size") int page_size, @RequestParam("current_page") int current_page){
        return new JsonResult<>(bookService.getAllBook(page_size, current_page ));
    }

    @PostMapping("/search")
    public JsonResult<ArrayList<Map<String, String>>> searchBook(@RequestBody Map<String, String> bookMessage) {

        BookDetails bookDetails = new BookDetails();
        bookDetails.setBookName(bookMessage.get("book_name"));
        bookDetails.setAuthor(bookMessage.get("author"));
        bookDetails.setPublisher(bookMessage.get("publisher"));
        bookDetails.setISBN(bookMessage.get("ISBN"));
        bookDetails.setCategory(bookMessage.get("category"));

        String publishYear = bookMessage.get("pub_year");
        String publishMonth = bookMessage.get("pub_month");
        Integer y = 0;
        Integer m = 0;
        if(!(publishMonth.equals("")) && !(publishYear.equals(""))) {
            y = Integer.valueOf(publishYear);
            m = Integer.valueOf(publishMonth);
            bookDetails.setPublishedDate(new Date(y - 1900, m - 1, 1));
            System.out.println(bookDetails);
        } else {
            bookDetails.setPublishedDate(null);
        }
        Integer formatCode = formatUtil.dateFormat(y, m, 0, 0);
        if (!formatCode.equals(0)) {
            return new JsonResult<>(null, formatCode, formatUtil.getErrorMap().get(formatCode));
        }
        ArrayList<Map<String, String>> books = null;
        try {
            books = (ArrayList<Map<String, String>>) bookInfoService.searchBookByMessage(bookDetails);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult<>(books, -1, "unknow error!");
        }
        return new JsonResult<>(books, 0, "Search suessfully!");
    }

    //获取都被借完（库存不够）的图书信息
    @GetMapping("/searchBorrowedBook")
    public JsonResult<JSONArray> searchBorrowedBook() {

        return new JsonResult<>(bookService.getBorrowedBook());
    }

    //获取最受欢迎的图书信息
    @GetMapping("/searchMostPopularBook")
    public JsonResult<JSONArray> searchMostPopularBook() {

        return new JsonResult<>(bookService.getMostPopularBook());
    }
    // 获取某一本书的信息
    @GetMapping("/book/detail")
    public JsonResult<JSONArray> getABook(@RequestParam("ISBN") String ISBN){
        return new JsonResult<>(bookService.getABook(ISBN));
    }

    // 更新图书
    @RequestMapping(value = "/book/update", method = RequestMethod.PUT)
    public JsonResult<JSONArray> updateBook(@RequestBody Book book){
        bookService.updateBook(book);
        return new JsonResult<>();
    }


    @PostMapping("/book/add")
    public JsonResult<JSONArray> addBook(@RequestBody Map map){
        bookService.addBook(map);
        return new JsonResult<>();
    }

    @RequestMapping(value = "/book/delete", method = RequestMethod.DELETE)
    public JsonResult<JSONArray> deleteBook(@RequestBody List<String> ISBNS ){
        bookService.delete(ISBNS);
        return new JsonResult<>();
    }

    @PostMapping("/bookbook/add")
    public JsonResult addBookBook(@RequestBody Map map){
        bookService.addBookBook(map);
        return new JsonResult();
    }









}
